package org.example.Servlet.PersonServlet;

import org.example.Dao.PersonDao;
import org.example.entity.Person;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/admin/person/editPerson")
public class EditPersonServlet extends HttpServlet {


    /**
     * 跳转到用户修改页面
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pid = Integer.parseInt(req.getParameter("pid"));
        HttpSession session = req.getSession();
        Person person = null;
        // 获取该用户信息
        PersonDao personDao = new PersonDao();
        try {
            person = personDao.getPersonByPid(pid);
            System.out.println("wait edit person info ==> " + person.toString());
            session.setAttribute("editPerson", person);
            session.setAttribute("editPersonMsg", "");
            resp.sendRedirect(req.getContextPath() + "/views/admin/edit.jsp");
        } catch (Exception e) {
            System.out.println("edit person error ==> " + e.getMessage());
            session.setAttribute("editPersonMsg", "编辑错误");
            if (person.getLevel() == 1) {
                req.getRequestDispatcher("/views/admin/index.jsp").forward(req, resp);
            } else {
                req.getRequestDispatcher("/views/user/index.jsp").forward(req, resp);
            }
        }


    }
}